#include <bits/stdc++.h>
using namespace std;

vector<int> primes;
const int limit = 1e3;

void shai() {
  vector<bool> isPrime(limit + 1, true);
  for (int i = 2; i <= limit; i++) {
    if (isPrime[i])
      primes.push_back(i);
    for (int p : primes) {
      if (i * p > limit)
        break;
      isPrime[i * p] = false;
      if (i % p == 0)
        break;
    }
  }
}

int solve(vector<int> &a, int m) {
  bool all_same = true;
  for (int i = 0; i < a.size(); i++)
    if (a[i] != a[0]) {
      all_same = false;
      break;
    }
  if (all_same)
    return 0;

  int total_gcd = a[0];
  for (int x : a)
    total_gcd = __gcd(total_gcd, x);

  if (total_gcd == 1)
    return 2; // 所有元素互质

  // 只要有共同因数(total_gcd>1)，就一定能找到共同质因数
  // 所以以下循环必定会返回1
  for (int p : primes) {
    if (p > total_gcd)
      break;
    if (total_gcd % p == 0) {
      bool all_contain = true;
      for (int x : a)
        if (x % p != 0) {
          all_contain = false;
          break;
        }
      if (all_contain)
        return 1;
    }
  }
  return -1; // 这个情况不会发生
}

int main() {
  shai();
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int &x : a)
      cin >> x;

    cout << solve(a, m) << endl;
  }
  return 0;
}
